package craky.keeper;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JComponent;

import craky.componentc.JCToggleButton;
import craky.keeper.allpay.AllPay;
import craky.keeper.allpay.AllPayPane;
import craky.keeper.user.User;
import craky.keeper.util.KeeperUtil;
import craky.util.Config;

public class KeeperMgr implements ActionListener, ItemListener {
	private KeeperApp keeper;
	private MainFrame mainFrame;
	private KeeperPane currentPane;
	private Config config;

	public KeeperMgr(KeeperApp keeper, MainFrame mainFrame) {
		this.keeper = keeper;
		this.mainFrame = mainFrame;
		this.config = keeper.getConfig();
	}

	public void showDetail(boolean show) {
		this.config.savePropertie("Show_Detail", String.valueOf(show));

		if (this.currentPane != null) {
			this.currentPane.setShowDetail(show);
		}
	}

	public void showFind(boolean show) {
		this.config.savePropertie("Show_Find", String.valueOf(show));

		if (this.currentPane != null) {
			this.currentPane.setShowFind(show);
		}
	}

	public void setTitleOpaque(boolean opaque) {
		this.mainFrame.setTitleOpaque(opaque);
		this.config.savePropertie("Title_Opaque", String.valueOf(opaque));
	}

	public void export() {
		if (this.currentPane != null) {
			KeeperUtil.switchToSystemLAFTemp(new AbstractAction() {
				private static final long serialVersionUID = -9177012597857998858L;

				public void actionPerformed(ActionEvent e) {
					KeeperMgr.this.currentPane.exportToCSV();
				}
			});
		}
	}

	public void reload() {
		if (this.currentPane != null) {
			this.currentPane.load();
		}
	}

	public void submit() {
		if (this.currentPane != null) {
			this.currentPane.submit();
		}
	} 

	public void setting() {
		new SettingDialog(this.keeper);
	}

	public void needReload(String type) {
		if ((this.currentPane instanceof AllPayPane)) {
			KeeperPane pane = this.mainFrame.getPaneByType(type);

			if (pane != null) {
				pane.needReload();
			}
		} else {
			this.mainFrame.getPaneByType("全部").needReload();
		}
	}

	public void resetTotal() {
		int total = 0;
		float amount = 0.0F;
		List<AllPay> datas;
		if ((this.currentPane != null)
				&& ((datas = this.currentPane.getAllData()) != null)) {
			total = datas.size();

			for (AllPay pay : datas) {
				amount += pay.getAmount();
			}
		}

		this.mainFrame.resetTotal(total, amount);
	}

	public KeeperPane getCurrentPane() {
		return this.currentPane;
	}

	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		String cmd = e.getActionCommand();
		User user = this.keeper.getCurrentUser();

		if (cmd.equals("CancelAutoLogin")) {
			Config loginConfig = this.keeper.getLoginConfig();
			loginConfig.removeAndSave(user.getName() + ".auto");
			((JComponent) source).setVisible(false);
		} else if (cmd.equals("CancelSavedPassword")) {
			Config loginConfig = this.keeper.getLoginConfig();
			Object linkedComponent = ((JComponent) source)
					.getClientProperty("LinkedComponent");
			loginConfig.removeAndSave(user.getName() + ".password");
			((JComponent) source).setVisible(false);

			if (linkedComponent != null) {
				((JComponent) linkedComponent).setVisible(false);
			}
		} else if (cmd.equals("Reload")) {
			reload();
		} else if (cmd.equals("Setting")) {
			setting();
		}
	}

	public void itemStateChanged(ItemEvent e) {
		Object source = e.getSource();

		if (((source instanceof JCToggleButton)) && (e.getStateChange() == 1)) {
			String cmd = ((JCToggleButton) source).getActionCommand();

			if (cmd.equals("ShowKeeperPane")) {
				this.currentPane = ((KeeperPane) ((JCToggleButton) source)
						.getClientProperty("LinkedComponent"));
				this.currentPane.setShowDetail(Boolean.valueOf(
						this.config.getProperty("Show_Detail", "true"))
						.booleanValue());
				this.currentPane.setShowFind(Boolean.valueOf(
						this.config.getProperty("Show_Find", "true"))
						.booleanValue());
				this.mainFrame.showPane(this.currentPane.getName());

				if (!this.currentPane.isInited()) {
					this.currentPane.load();
				} else {
					resetTotal();

					if (this.currentPane != null) {
						this.currentPane.refreshStat();
					}
				}
			}
		}
	}
}